package com.xy.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author xxy
 * @date 2022/1/7 13:13
 */
@RestController
@Slf4j
@RequestMapping("/use-hashMap")
public class UseHashMapController {

    /**
     * 用作缓存的容器
     */
    private Map<String, Object> cache = new HashMap<>();

    @GetMapping("/test-cache")
    public Object testUseHashMapCache() {
        // 先查缓存
        Object dataList = cache.get("dataList");
        // 如果缓存为 null
        if (dataList == null) {
            log.info("the cache is empty!");
            // 再查询数据库
            // 这里模拟查询的是数据库
            List<String> list = dataList();
            dataList = list;
            cache.put("dataList", list);
        }
        log.info("dataList -> {}", dataList);
        return dataList;
    }

    private List<String> dataList() {
        List<String> dataList = new ArrayList<>();
        String[] data = {"a", "b", "c"};
        List<String> list = Arrays.asList(data);
        dataList.addAll(list);
        return dataList;
    }

//    public static void main(String[] args) {
//        dataList().stream().forEach(e->{
//            System.out.print(e + ", ");
//        });
//    }


}
